home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_kernel_source / FS / NAMEI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-17  |  32.3 KB  |  1,398 lines

  1. /*
  2.  *  linux/fs/namei.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  */
  6.  
  7. /*
  8.  * Some corrections by tytso.
  9.  */
  10.  
  11. /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
  12.  * lookup logic.
  13.  */
  14.  
  15. #include <linux/mm.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/smp_lock.h>
  18. #include <linux/quotaops.h>
  19.  
  20. #include <asm/uaccess.h>
  21. #include <asm/unaligned.h>
  22. #include <asm/semaphore.h>
  23. #include <asm/page.h>
  24. #include <asm/pgtable.h>
  25.  
  26. /*
  27.  * The bitmask for a lookup event:
  28.  *  - follow links at the end
  29.  *  - require a directory
  30.  *  - ending slashes ok even for nonexistent files
  31.  *  - internal "there are more path compnents" flag
  32.  */
  33. #define LOOKUP_FOLLOW        (1)
  34. #define LOOKUP_DIRECTORY    (2)
  35. #define LOOKUP_SLASHOK        (4)
  36. #define LOOKUP_CONTINUE        (8)
  37.  
  38. #include <asm/namei.h>
  39.  
  40. /* This can be removed after the beta phase. */
  41. #define CACHE_SUPERVISE    /* debug the correctness of dcache entries */
  42. #undef DEBUG        /* some other debugging */
  43.  
  44.  
  45. #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
  46.  
  47. /* [Feb-1997 T. Schoebel-Theuer]
  48.  * Fundamental changes in the pathname lookup mechanisms (namei)
  49.  * were necessary because of omirr.  The reason is that omirr needs
  50.  * to know the _real_ pathname, not the user-supplied one, in case
  51.  * of symlinks (and also when transname replacements occur).
  52.  *
  53.  * The new code replaces the old recursive symlink resolution with
  54.  * an iterative one (in case of non-nested symlink chains).  It does
  55.  * this with calls to <fs>_follow_link().
  56.  * As a side effect, dir_namei(), _namei() and follow_link() are now 
  57.  * replaced with a single function lookup_dentry() that can handle all 
  58.  * the special cases of the former code.
  59.  *
  60.  * With the new dcache, the pathname is stored at each inode, at least as
  61.  * long as the refcount of the inode is positive.  As a side effect, the
  62.  * size of the dcache depends on the inode cache and thus is dynamic.
  63.  *
  64.  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  65.  * resolution to correspond with current state of the code.
  66.  *
  67.  * Note that the symlink resolution is not *completely* iterative.
  68.  * There is still a significant amount of tail- and mid- recursion in
  69.  * the algorithm.  Also, note that <fs>_readlink() is not used in
  70.  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  71.  * may return different results than <fs>_follow_link().  Many virtual
  72.  * filesystems (including /proc) exhibit this behavior.
  73.  */
  74.  
  75. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  76.  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  77.  * and the name already exists in form of a symlink, try to create the new
  78.  * name indicated by the symlink. The old code always complained that the
  79.  * name already exists, due to not following the symlink even if its target
  80.  * is nonexistent.  The new semantics affects also mknod() and link() when
  81.  * the name is a symlink pointing to a non-existant name.
  82.  *
  83.  * I don't know which semantics is the right one, since I have no access
  84.  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  85.  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  86.  * "old" one. Personally, I think the new semantics is much more logical.
  87.  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  88.  * file does succeed in both HP-UX and SunOs, but not in Solaris
  89.  * and in the old Linux semantics.
  90.  */
  91.  
  92. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  93.  * semantics.  See the comments in "open_namei" and "do_link" below.
  94.  *
  95.  * [10-Sep-98 Alan Modra] Another symlink change.
  96.  */
  97.  
  98. /* In order to reduce some races, while at the same time doing additional
  99.  * checking and hopefully speeding things up, we copy filenames to the
  100.  * kernel data space before using them..
  101.  *
  102.  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  103.  */
  104. static inline int do_getname(const char *filename, char *page)
  105. {
  106.     int retval;
  107.     unsigned long len = PAGE_SIZE;
  108.  
  109.     if ((unsigned long) filename >= TASK_SIZE) {
  110.         if (!segment_eq(get_fs(), KERNEL_DS))
  111.             return -EFAULT;
  112.     } else if (TASK_SIZE - (unsigned long) filename < PAGE_SIZE)
  113.         len = TASK_SIZE - (unsigned long) filename;
  114.  
  115.     retval = strncpy_from_user((char *)page, filename, len);
  116.     if (retval > 0) {
  117.         if (retval < len)
  118.             return 0;
  119.         return -ENAMETOOLONG;
  120.     } else if (!retval)
  121.         retval = -ENOENT;
  122.     return retval;
  123. }
  124.  
  125. char * getname(const char * filename)
  126. {
  127.     char *tmp, *result;
  128.  
  129.     result = ERR_PTR(-ENOMEM);
  130.     tmp = __getname();
  131.     if (tmp)  {
  132.         int retval = do_getname(filename, tmp);
  133.  
  134.         result = tmp;
  135.         if (retval < 0) {
  136.             putname(tmp);
  137.             result = ERR_PTR(retval);
  138.         }
  139.     }
  140.     return result;
  141. }
  142.  
  143. /*
  144.  *    permission()
  145.  *
  146.  * is used to check for read/write/execute permissions on a file.
  147.  * We use "fsuid" for this, letting us set arbitrary permissions
  148.  * for filesystem access without changing the "normal" uids which
  149.  * are used for other things..
  150.  */
  151. int permission(struct inode * inode,int mask)
  152. {
  153.     int mode = inode->i_mode;
  154.  
  155.     if (inode->i_op && inode->i_op->permission)
  156.         return inode->i_op->permission(inode, mask);
  157.     else if ((mask & S_IWOTH) && IS_RDONLY(inode) &&
  158.          (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  159.         return -EROFS; /* Nobody gets write access to a read-only fs */
  160.     else if ((mask & S_IWOTH) && IS_IMMUTABLE(inode))
  161.         return -EACCES; /* Nobody gets write access to an immutable file */
  162.     else if (current->fsuid == inode->i_uid)
  163.         mode >>= 6;
  164.     else if (in_group_p(inode->i_gid))
  165.         mode >>= 3;
  166.     if (((mode & mask & S_IRWXO) == mask) || capable(CAP_DAC_OVERRIDE))
  167.         return 0;
  168.     /* read and search access */
  169.     if ((mask == S_IROTH) ||
  170.         (S_ISDIR(mode)  && !(mask & ~(S_IROTH | S_IXOTH))))
  171.         if (capable(CAP_DAC_READ_SEARCH))
  172.             return 0;
  173.     return -EACCES;
  174. }
  175.  
  176. /*
  177.  * get_write_access() gets write permission for a file.
  178.  * put_write_access() releases this write permission.
  179.  * This is used for regular files.
  180.  * We cannot support write (and maybe mmap read-write shared) accesses and
  181.  * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
  182.  * can have the following values:
  183.  * 0: no writers, no VM_DENYWRITE mappings
  184.  * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
  185.  * > 0: (i_writecount) users are writing to the file.
  186.  */
  187. int get_write_access(struct inode * inode)
  188. {
  189.     if (inode->i_writecount < 0)
  190.         return -ETXTBSY;
  191.     inode->i_writecount++;
  192.     return 0;
  193. }
  194.  
  195. void put_write_access(struct inode * inode)
  196. {
  197.     inode->i_writecount--;
  198. }
  199.  
  200. /*
  201.  * "." and ".." are special - ".." especially so because it has to be able
  202.  * to know about the current root directory and parent relationships
  203.  */
  204. static struct dentry * reserved_lookup(struct dentry * parent, struct qstr * name)
  205. {
  206.     struct dentry *result = NULL;
  207.     if (name->name[0] == '.') {
  208.         switch (name->len) {
  209.         default:
  210.             break;
  211.         case 2:    
  212.             if (name->name[1] != '.')
  213.                 break;
  214.  
  215.             if (parent != current->fs->root)
  216.                 parent = parent->d_covers->d_parent;
  217.             /* fallthrough */
  218.         case 1:
  219.             result = parent;
  220.         }
  221.     }
  222.     return dget(result);
  223. }
  224.  
  225. /*
  226.  * Internal lookup() using the new generic dcache.
  227.  */
  228. static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name)
  229. {
  230.     struct dentry * dentry = d_lookup(parent, name);
  231.  
  232.     if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
  233.         if (!dentry->d_op->d_revalidate(dentry) && !d_invalidate(dentry)) {
  234.             dput(dentry);
  235.             dentry = NULL;
  236.         }
  237.     }
  238.     return dentry;
  239. }
  240.  
  241. /*
  242.  * This is called when everything else fails, and we actually have
  243.  * to go to the low-level filesystem to find out what we should do..
  244.  *
  245.  * We get the directory semaphore, and after getting that we also
  246.  * make sure that nobody added the entry to the dcache in the meantime..
  247.  */
  248. static struct dentry * real_lookup(struct dentry * parent, struct qstr * name)
  249. {
  250.     struct dentry * result;
  251.     struct inode *dir = parent->d_inode;
  252.  
  253.     down(&dir->i_sem);
  254.     /*
  255.      * First re-do the cached lookup just in case it was created
  256.      * while we waited for the directory semaphore..
  257.      *
  258.      * FIXME! This could use version numbering or similar to
  259.      * avoid unnecessary cache lookups.
  260.      */
  261.     result = cached_lookup(parent, name);
  262.     if (!result) {
  263.         struct dentry * dentry = d_alloc(parent, name);
  264.         result = ERR_PTR(-ENOMEM);
  265.         if (dentry) {
  266.             int error = dir->i_op->lookup(dir, dentry);
  267.             result = dentry;
  268.             if (error) {
  269.                 dput(dentry);
  270.                 result = ERR_PTR(error);
  271.             }
  272.         }
  273.     }
  274.     up(&dir->i_sem);
  275.     return result;
  276. }
  277.  
  278. static struct dentry * do_follow_link(struct dentry *base, struct dentry *dentry, unsigned int follow)
  279. {
  280.     struct inode * inode = dentry->d_inode;
  281.  
  282.     if ((follow & LOOKUP_FOLLOW)
  283.         && inode && inode->i_op && inode->i_op->follow_link) {
  284.         if (current->link_count < 5) {
  285.             struct dentry * result;
  286.  
  287.             current->link_count++;
  288.             /* This eats the base */
  289.             result = inode->i_op->follow_link(dentry, base, follow);
  290.             current->link_count--;
  291.             dput(dentry);
  292.             return result;
  293.         }
  294.         dput(dentry);
  295.         dentry = ERR_PTR(-ELOOP);
  296.     }
  297.     dput(base);
  298.     return dentry;
  299. }
  300.  
  301. static inline struct dentry * follow_mount(struct dentry * dentry)
  302. {
  303.     struct dentry * mnt = dentry->d_mounts;
  304.  
  305.     if (mnt != dentry) {
  306.         dget(mnt);
  307.         dput(dentry);
  308.         dentry = mnt;
  309.     }
  310.     return dentry;
  311. }
  312.  
  313. /*
  314.  * Name resolution.
  315.  *
  316.  * This is the basic name resolution function, turning a pathname
  317.  * into the final dentry.
  318.  */
  319. struct dentry * lookup_dentry(const char * name, struct dentry * base, unsigned int lookup_flags)
  320. {
  321.     struct dentry * dentry;
  322.     struct inode *inode;
  323.  
  324.     if (*name == '/') {
  325.         if (base)
  326.             dput(base);
  327.         do {
  328.             name++;
  329.         } while (*name == '/');
  330.         __prefix_lookup_dentry(name, lookup_flags);
  331.         base = dget(current->fs->root);
  332.     } else if (!base) {
  333.         base = dget(current->fs->pwd);
  334.     }
  335.  
  336.     if (!*name)
  337.         goto return_base;
  338.  
  339.     inode = base->d_inode;
  340.     lookup_flags &= LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_SLASHOK;
  341.  
  342.     /* At this point we know we have a real path component. */
  343.     for(;;) {
  344.         int err;
  345.         unsigned long hash;
  346.         struct qstr this;
  347.         unsigned int flags;
  348.         unsigned int c;
  349.  
  350.         err = permission(inode, MAY_EXEC);
  351.         dentry = ERR_PTR(err);
  352.          if (err)
  353.             break;
  354.  
  355.         this.name = name;
  356.         c = *(const unsigned char *)name;
  357.  
  358.         hash = init_name_hash();
  359.         do {
  360.             name++;
  361.             hash = partial_name_hash(c, hash);
  362.             c = *(const unsigned char *)name;
  363.         } while (c && (c != '/'));
  364.         this.len = name - (const char *) this.name;
  365.         this.hash = end_name_hash(hash);
  366.  
  367.         /* remove trailing slashes? */
  368.         flags = lookup_flags;
  369.         if (c) {
  370.             char tmp;
  371.  
  372.             flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  373.             do {
  374.                 tmp = *++name;
  375.             } while (tmp == '/');
  376.             if (tmp)
  377.                 flags |= LOOKUP_CONTINUE;
  378.         }
  379.  
  380.         /*
  381.          * See if the low-level filesystem might want
  382.          * to use its own hash..
  383.          */
  384.         if (base->d_op && base->d_op->d_hash) {
  385.             int error;
  386.             error = base->d_op->d_hash(base, &this);
  387.             if (error < 0) {
  388.                 dentry = ERR_PTR(error);
  389.                 break;
  390.             }
  391.         }
  392.  
  393.         /* This does the actual lookups.. */
  394.         dentry = reserved_lookup(base, &this);
  395.         if (!dentry) {
  396.             dentry = cached_lookup(base, &this);
  397.             if (!dentry) {
  398.                 dentry = real_lookup(base, &this);
  399.                 if (IS_ERR(dentry))
  400.                     break;
  401.             }
  402.         }
  403.  
  404.         /* Check mountpoints.. */
  405.         dentry = follow_mount(dentry);
  406.  
  407.         base = do_follow_link(base, dentry, flags);
  408.         if (IS_ERR(base))
  409.             goto return_base;
  410.  
  411.         inode = base->d_inode;
  412.         if (flags & LOOKUP_DIRECTORY) {
  413.             if (!inode)
  414.                 goto no_inode;
  415.             dentry = ERR_PTR(-ENOTDIR); 
  416.             if (!inode->i_op || !inode->i_op->lookup)
  417.                 break;
  418.             if (flags & LOOKUP_CONTINUE)
  419.                 continue;
  420.         }
  421. return_base:
  422.         return base;
  423. /*
  424.  * The case of a nonexisting file is special.
  425.  *
  426.  * In the middle of a pathname lookup (ie when
  427.  * LOOKUP_CONTINUE is set), it's an obvious
  428.  * error and returns ENOENT.
  429.  *
  430.  * At the end of a pathname lookup it's legal,
  431.  * and we return a negative dentry. However, we
  432.  * get here only if there were trailing slashes,
  433.  * which is legal only if we know it's supposed
  434.  * to be a directory (ie "mkdir"). Thus the
  435.  * LOOKUP_SLASHOK flag.
  436.  */
  437. no_inode:
  438.         dentry = ERR_PTR(-ENOENT);
  439.         if (flags & LOOKUP_CONTINUE)
  440.             break;
  441.         if (flags & LOOKUP_SLASHOK)
  442.             goto return_base;
  443.         break;
  444.     }
  445.     dput(base);
  446.     return dentry;
  447. }
  448.  
  449. /*
  450.  *    namei()
  451.  *
  452.  * is used by most simple commands to get the inode of a specified name.
  453.  * Open, link etc use their own routines, but this is enough for things
  454.  * like 'chmod' etc.
  455.  *
  456.  * namei exists in two versions: namei/lnamei. The only difference is
  457.  * that namei follows links, while lnamei does not.
  458.  */
  459. struct dentry * __namei(const char *pathname, unsigned int lookup_flags)
  460. {
  461.     char *name;
  462.     struct dentry *dentry;
  463.  
  464.     name = getname(pathname);
  465.     dentry = (struct dentry *) name;
  466.     if (!IS_ERR(name)) {
  467.         dentry = lookup_dentry(name, NULL, lookup_flags);
  468.         putname(name);
  469.         if (!IS_ERR(dentry)) {
  470.             if (!dentry->d_inode) {
  471.                 dput(dentry);
  472.                 dentry = ERR_PTR(-ENOENT);
  473.             }
  474.         }
  475.     }
  476.     return dentry;
  477. }
  478.  
  479. /*
  480.  * It's inline, so penalty for filesystems that don't use sticky bit is
  481.  * minimal.
  482.  */
  483. static inline int check_sticky(struct inode *dir, struct inode *inode)
  484. {
  485.     if (!(dir->i_mode & S_ISVTX))
  486.         return 0;
  487.     if (inode->i_uid == current->fsuid)
  488.         return 0;
  489.     if (dir->i_uid == current->fsuid)
  490.         return 0;
  491.     return !capable(CAP_FOWNER);
  492. }
  493.  
  494. /*
  495.  *    Check whether we can remove a link victim from directory dir, check
  496.  *  whether the type of victim is right.
  497.  *  1. We can't do it if dir is read-only (done in permission())
  498.  *  2. We should have write and exec permissions on dir
  499.  *  3. We can't remove anything from append-only dir
  500.  *  4. We can't do anything with immutable dir (done in permission())
  501.  *  5. If the sticky bit on dir is set we should either
  502.  *    a. be owner of dir, or
  503.  *    b. be owner of victim, or
  504.  *    c. have CAP_FOWNER capability
  505.  *  6. If the victim is append-only or immutable we can't do antyhing with
  506.  *     links pointing to it.
  507.  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  508.  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  509.  *  9. We can't remove a root or mountpoint.
  510.  */
  511. static inline int may_delete(struct inode *dir,struct dentry *victim, int isdir)
  512. {
  513.     int error;
  514.     if (!victim->d_inode || victim->d_parent->d_inode != dir)
  515.         return -ENOENT;
  516.     error = permission(dir,MAY_WRITE | MAY_EXEC);
  517.     if (error)
  518.         return error;
  519.     if (IS_APPEND(dir))
  520.         return -EPERM;
  521.     if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  522.         IS_IMMUTABLE(victim->d_inode))
  523.         return -EPERM;
  524.     if (isdir) {
  525.         if (!S_ISDIR(victim->d_inode->i_mode))
  526.             return -ENOTDIR;
  527.         if (IS_ROOT(victim))
  528.             return -EBUSY;
  529.         if (victim->d_mounts != victim->d_covers)
  530.             return -EBUSY;
  531.     } else if (S_ISDIR(victim->d_inode->i_mode))
  532.         return -EISDIR;
  533.     return 0;
  534. }
  535.  
  536. /*    Check whether we can create an object with dentry child in directory
  537.  *  dir.
  538.  *  1. We can't do it if child already exists (open has special treatment for
  539.  *     this case, but since we are inlined it's OK)
  540.  *  2. We can't do it if dir is read-only (done in permission())
  541.  *  3. We should have write and exec permissions on dir
  542.  *  4. We can't do it if dir is immutable (done in permission())
  543.  */
  544. static inline int may_create(struct inode *dir, struct dentry *child) {
  545.     if (child->d_inode)
  546.         return -EEXIST;
  547.     return permission(dir,MAY_WRITE | MAY_EXEC);
  548. }
  549.  
  550. static inline struct dentry *get_parent(struct dentry *dentry)
  551. {
  552.     return dget(dentry->d_parent);
  553. }
  554.  
  555. static inline void unlock_dir(struct dentry *dir)
  556. {
  557.     up(&dir->d_inode->i_sem);
  558.     dput(dir);
  559. }
  560.  
  561. /*
  562.  * We need to do a check-parent every time
  563.  * after we have locked the parent - to verify
  564.  * that the parent is still our parent and
  565.  * that we are still hashed onto it..
  566.  *
  567.  * This is requied in case two processes race
  568.  * on removing (or moving) the same entry: the
  569.  * parent lock will serialize them, but the
  570.  * other process will be too late..
  571.  */
  572. #define check_parent(dir, dentry) \
  573.     ((dir) == (dentry)->d_parent && !list_empty(&dentry->d_hash))
  574.  
  575. /*
  576.  * Locking the parent is needed to:
  577.  *  - serialize directory operations
  578.  *  - make sure the parent doesn't change from
  579.  *    under us in the middle of an operation.
  580.  *
  581.  * NOTE! Right now we'd rather use a "struct inode"
  582.  * for this, but as I expect things to move toward
  583.  * using dentries instead for most things it is
  584.  * probably better to start with the conceptually
  585.  * better interface of relying on a path of dentries.
  586.  */
  587. static inline struct dentry *lock_parent(struct dentry *dentry)
  588. {
  589.     struct dentry *dir = dget(dentry->d_parent);
  590.  
  591.     down(&dir->d_inode->i_sem);
  592.     return dir;
  593. }
  594.  
  595. /*
  596.  * Whee.. Deadlock country. Happily there are only two VFS
  597.  * operations that do this..
  598.  */
  599. static inline void double_lock(struct dentry *d1, struct dentry *d2)
  600. {
  601.     struct semaphore *s1 = &d1->d_inode->i_sem;
  602.     struct semaphore *s2 = &d2->d_inode->i_sem;
  603.  
  604.     if (s1 != s2) {
  605.         if ((unsigned long) s1 < (unsigned long) s2) {
  606.             struct semaphore *tmp = s2;
  607.             s2 = s1; s1 = tmp;
  608.         }
  609.         down(s1);
  610.     }
  611.     down(s2);
  612. }
  613.  
  614. static inline void double_unlock(struct dentry *d1, struct dentry *d2)
  615. {
  616.     struct semaphore *s1 = &d1->d_inode->i_sem;
  617.     struct semaphore *s2 = &d2->d_inode->i_sem;
  618.  
  619.     up(s1);
  620.     if (s1 != s2)
  621.         up(s2);
  622.     dput(d1);
  623.     dput(d2);
  624. }
  625.  
  626.  
  627. /* 
  628.  * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
  629.  * reasons.
  630.  *
  631.  * O_DIRECTORY translates into forcing a directory lookup.
  632.  */
  633. static inline int lookup_flags(unsigned int f)
  634. {
  635.     unsigned long retval = LOOKUP_FOLLOW;
  636.  
  637.     if (f & O_NOFOLLOW)
  638.         retval &= ~LOOKUP_FOLLOW;
  639.     
  640.     if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  641.         retval &= ~LOOKUP_FOLLOW;
  642.     
  643.     if (f & O_DIRECTORY)
  644.         retval |= LOOKUP_DIRECTORY;
  645.     
  646.     return retval;
  647. }
  648.  
  649. /*
  650.  *    open_namei()
  651.  *
  652.  * namei for open - this is in fact almost the whole open-routine.
  653.  *
  654.  * Note that the low bits of "flag" aren't the same as in the open
  655.  * system call - they are 00 - no permissions needed
  656.  *              01 - read permission needed
  657.  *              10 - write permission needed
  658.  *              11 - read/write permissions needed
  659.  * which is a lot more logical, and also allows the "no perm" needed
  660.  * for symlinks (where the permissions are checked later).
  661.  */
  662. struct dentry * open_namei(const char * pathname, int flag, int mode)
  663. {
  664.     int acc_mode, error;
  665.     struct inode *inode;
  666.     struct dentry *dentry;
  667.  
  668.     mode &= S_IALLUGO & ~current->fs->umask;
  669.     mode |= S_IFREG;
  670.  
  671.     dentry = lookup_dentry(pathname, NULL, lookup_flags(flag));
  672.     if (IS_ERR(dentry))
  673.         return dentry;
  674.  
  675.     acc_mode = ACC_MODE(flag);
  676.     if (flag & O_CREAT) {
  677.         struct dentry *dir;
  678.  
  679.         if (dentry->d_inode) {
  680.             if (!(flag & O_EXCL))
  681.                 goto nocreate;
  682.             error = -EEXIST;
  683.             goto exit;
  684.         }
  685.  
  686.         dir = lock_parent(dentry);
  687.         if (!check_parent(dir, dentry)) {
  688.             /*
  689.              * Really nasty race happened. What's the 
  690.              * right error code? We had a dentry, but
  691.              * before we could use it it was removed
  692.              * by somebody else. We could just re-try
  693.              * everything, I guess.
  694.              *
  695.              * ENOENT is definitely wrong.
  696.              */
  697.             error = -ENOENT;
  698.             unlock_dir(dir);
  699.             goto exit;
  700.         }
  701.  
  702.         /*
  703.          * Somebody might have created the file while we
  704.          * waited for the directory lock.. So we have to
  705.          * re-do the existence test.
  706.          */
  707.         if (dentry->d_inode) {
  708.             error = 0;
  709.             if (flag & O_EXCL)
  710.                 error = -EEXIST;
  711.         } else if ((error = may_create(dir->d_inode, dentry)) == 0) {
  712.             if (!dir->d_inode->i_op || !dir->d_inode->i_op->create)
  713.                 error = -EACCES;
  714.             else {
  715.                 DQUOT_INIT(dir->d_inode);
  716.                 error = dir->d_inode->i_op->create(dir->d_inode, dentry, mode);
  717.                 /* Don't check for write permission, don't truncate */
  718.                 acc_mode = 0;
  719.                 flag &= ~O_TRUNC;
  720.             }
  721.         }
  722.         unlock_dir(dir);
  723.         if (error)
  724.             goto exit;
  725.     }
  726.  
  727. nocreate:
  728.     error = -ENOENT;
  729.     inode = dentry->d_inode;
  730.     if (!inode)
  731.         goto exit;
  732.  
  733.     error = -ELOOP;
  734.     if (S_ISLNK(inode->i_mode))
  735.         goto exit;
  736.     
  737.     error = -EISDIR;
  738.     if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
  739.         goto exit;
  740.  
  741.     error = permission(inode,acc_mode);
  742.     if (error)
  743.         goto exit;
  744.  
  745.     /*
  746.      * FIFO's, sockets and device files are special: they don't
  747.      * actually live on the filesystem itself, and as such you
  748.      * can write to them even if the filesystem is read-only.
  749.      */
  750.     if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  751.             flag &= ~O_TRUNC;
  752.     } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
  753.         error = -EACCES;
  754.         if (IS_NODEV(inode))
  755.             goto exit;
  756.  
  757.         flag &= ~O_TRUNC;
  758.     } else {
  759.         error = -EROFS;
  760.         if (IS_RDONLY(inode) && (flag & 2))
  761.             goto exit;
  762.     }
  763.     /*
  764.      * An append-only file must be opened in append mode for writing.
  765.      */
  766.     error = -EPERM;
  767.     if (IS_APPEND(inode)) {
  768.         if  ((flag & FMODE_WRITE) && !(flag & O_APPEND))
  769.             goto exit;
  770.         if (flag & O_TRUNC)
  771.             goto exit;
  772.     }
  773.  
  774.     if (flag & O_TRUNC) {
  775.         error = get_write_access(inode);
  776.         if (error)
  777.             goto exit;
  778.  
  779.         /*
  780.          * Refuse to truncate files with mandatory locks held on them.
  781.          */
  782.         error = locks_verify_locked(inode);
  783.         if (!error) {
  784.             DQUOT_INIT(inode);
  785.             
  786.             error = do_truncate(dentry, 0);
  787.         }
  788.         put_write_access(inode);
  789.         if (error)
  790.             goto exit;
  791.     } else
  792.         if (flag & FMODE_WRITE)
  793.             DQUOT_INIT(inode);
  794.  
  795.     return dentry;
  796.  
  797. exit:
  798.     dput(dentry);
  799.     return ERR_PTR(error);
  800. }
  801.  
  802. struct dentry * do_mknod(const char * filename, int mode, dev_t dev)
  803. {
  804.     int error;
  805.     struct dentry *dir;
  806.     struct dentry *dentry, *retval;
  807.  
  808.     mode &= ~current->fs->umask;
  809.     dentry = lookup_dentry(filename, NULL, LOOKUP_FOLLOW);
  810.     if (IS_ERR(dentry))
  811.         return dentry;
  812.  
  813.     dir = lock_parent(dentry);
  814.     error = -ENOENT;
  815.     if (!check_parent(dir, dentry))
  816.         goto exit_lock;
  817.  
  818.     error = may_create(dir->d_inode, dentry);
  819.     if (error)
  820.         goto exit_lock;
  821.  
  822.     error = -EPERM;
  823.     if (!dir->d_inode->i_op || !dir->d_inode->i_op->mknod)
  824.         goto exit_lock;
  825.  
  826.     DQUOT_INIT(dir->d_inode);
  827.     error = dir->d_inode->i_op->mknod(dir->d_inode, dentry, mode, dev);
  828. exit_lock:
  829.     retval = ERR_PTR(error);
  830.     if (!error)
  831.         retval = dget(dentry);
  832.     unlock_dir(dir);
  833.     dput(dentry);
  834.     return retval;
  835. }
  836.  
  837. asmlinkage int sys_mknod(const char * filename, int mode, dev_t dev)
  838. {
  839.     int error;
  840.     char * tmp;
  841.  
  842.     lock_kernel();
  843.     error = -EPERM;
  844.     if (S_ISDIR(mode) || (!S_ISFIFO(mode) && !capable(CAP_SYS_ADMIN)))
  845.         goto out;
  846.     error = -EINVAL;
  847.     switch (mode & S_IFMT) {
  848.     case 0:
  849.         mode |= S_IFREG;
  850.         break;
  851.     case S_IFREG: case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
  852.         break;
  853.     default:
  854.         goto out;
  855.     }
  856.     tmp = getname(filename);
  857.     error = PTR_ERR(tmp);
  858.     if (!IS_ERR(tmp)) {
  859.         struct dentry * dentry = do_mknod(tmp,mode,dev);
  860.         putname(tmp);
  861.         error = PTR_ERR(dentry);
  862.         if (!IS_ERR(dentry)) {
  863.             dput(dentry);
  864.             error = 0;
  865.         }
  866.     }
  867. out:
  868.     unlock_kernel();
  869.     return error;
  870. }
  871.  
  872. /*
  873.  * Look out: this function may change a normal dentry
  874.  * into a directory dentry (different size)..
  875.  */
  876. static inline int do_mkdir(const char * pathname, int mode)
  877. {
  878.     int error;
  879.     struct dentry *dir;
  880.     struct dentry *dentry;
  881.  
  882.     dentry = lookup_dentry(pathname, NULL, LOOKUP_SLASHOK);
  883.     error = PTR_ERR(dentry);
  884.     if (IS_ERR(dentry))
  885.         goto exit;
  886.  
  887.     /*
  888.      * EEXIST is kind of a strange error code to
  889.      * return, but basically if the dentry was moved
  890.      * or unlinked while we locked the parent, we
  891.      * do know that it _did_ exist before, and as
  892.      * such it makes perfect sense.. In contrast,
  893.      * ENOENT doesn't make sense for mkdir.
  894.      */
  895.     dir = lock_parent(dentry);
  896.     error = -EEXIST;
  897.     if (!check_parent(dir, dentry))
  898.         goto exit_lock;
  899.  
  900.     error = may_create(dir->d_inode, dentry);
  901.     if (error)
  902.         goto exit_lock;
  903.  
  904.     error = -EPERM;
  905.     if (!dir->d_inode->i_op || !dir->d_inode->i_op->mkdir)
  906.         goto exit_lock;
  907.  
  908.     DQUOT_INIT(dir->d_inode);
  909.     mode &= 0777 & ~current->fs->umask;
  910.     error = dir->d_inode->i_op->mkdir(dir->d_inode, dentry, mode);
  911.  
  912. exit_lock:
  913.     unlock_dir(dir);
  914.     dput(dentry);
  915. exit:
  916.     return error;
  917. }
  918.  
  919. asmlinkage int sys_mkdir(const char * pathname, int mode)
  920. {
  921.     int error;
  922.     char * tmp;
  923.  
  924.     lock_kernel();
  925.     tmp = getname(pathname);
  926.     error = PTR_ERR(tmp);
  927.     if (!IS_ERR(tmp)) {
  928.         error = do_mkdir(tmp,mode);
  929.         putname(tmp);
  930.     }
  931.     unlock_kernel();
  932.     return error;
  933. }
  934.  
  935. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  936. {
  937.     int error;
  938.  
  939.     error = may_delete(dir, dentry, 1);
  940.     if (error)
  941.         return error;
  942.  
  943.     if (!dir->i_op || !dir->i_op->rmdir)
  944.         return -EPERM;
  945.  
  946.     DQUOT_INIT(dir);
  947.  
  948.     /*
  949.      * We try to drop the dentry early: we should have
  950.      * a usage count of 2 if we're the only user of this
  951.      * dentry, and if that is true (possibly after pruning
  952.      * the dcache), then we drop the dentry now.
  953.      *
  954.      * A low-level filesystem can, if it choses, legally
  955.      * do a
  956.      *
  957.      *    if (!list_empty(&dentry->d_hash))
  958.      *        return -EBUSY;
  959.      *
  960.      * if it cannot handle the case of removing a directory
  961.      * that is still in use by something else..
  962.      */
  963.     switch (dentry->d_count) {
  964.     default:
  965.         shrink_dcache_parent(dentry);
  966.         if (dentry->d_count != 2)
  967.             break;
  968.     case 2:
  969.         d_drop(dentry);
  970.     }
  971.  
  972.     error = dir->i_op->rmdir(dir, dentry);
  973.  
  974.     return error;
  975. }
  976.  
  977. static inline int do_rmdir(const char * name)
  978. {
  979.     int error;
  980.     struct dentry *dir;
  981.     struct dentry *dentry;
  982.  
  983.     dentry = lookup_dentry(name, NULL, 0);
  984.     error = PTR_ERR(dentry);
  985.     if (IS_ERR(dentry))
  986.         goto exit;
  987.  
  988.     error = -ENOENT;
  989.     if (!dentry->d_inode)
  990.         goto exit_dput;
  991.  
  992.     dir = dget(dentry->d_parent);
  993.  
  994.     /*
  995.      * The dentry->d_count stuff confuses d_delete() enough to
  996.      * not kill the inode from under us while it is locked. This
  997.      * wouldn't be needed, except the dentry semaphore is really
  998.      * in the inode, not in the dentry..
  999.      */
  1000.     dentry->d_count++;
  1001.     double_lock(dir, dentry);
  1002.  
  1003.     error = -ENOENT;
  1004.     if (check_parent(dir, dentry))
  1005.         error = vfs_rmdir(dir->d_inode, dentry);
  1006.  
  1007.     double_unlock(dentry, dir);
  1008. exit_dput:
  1009.     dput(dentry);
  1010. exit:
  1011.     return error;
  1012. }
  1013.  
  1014. asmlinkage int sys_rmdir(const char * pathname)
  1015. {
  1016.     int error;
  1017.     char * tmp;
  1018.  
  1019.     lock_kernel();
  1020.     tmp = getname(pathname);
  1021.     error = PTR_ERR(tmp);
  1022.     if (!IS_ERR(tmp)) {
  1023.         error = do_rmdir(tmp);
  1024.         putname(tmp);
  1025.     }
  1026.     unlock_kernel();
  1027.     return error;
  1028. }
  1029.  
  1030. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  1031. {
  1032.     int error;
  1033.  
  1034.     error = may_delete(dir, dentry, 0);
  1035.     if (!error) {
  1036.         error = -EPERM;
  1037.         if (dir->i_op && dir->i_op->unlink) {
  1038.             DQUOT_INIT(dir);
  1039.             error = dir->i_op->unlink(dir, dentry);
  1040.         }
  1041.     }
  1042.     return error;
  1043. }
  1044.  
  1045. static inline int do_unlink(const char * name)
  1046. {
  1047.     int error;
  1048.     struct dentry *dir;
  1049.     struct dentry *dentry;
  1050.  
  1051.     dentry = lookup_dentry(name, NULL, 0);
  1052.     error = PTR_ERR(dentry);
  1053.     if (IS_ERR(dentry))
  1054.         goto exit;
  1055.  
  1056.     dir = lock_parent(dentry);
  1057.     error = -ENOENT;
  1058.     if (check_parent(dir, dentry))
  1059.         error = vfs_unlink(dir->d_inode, dentry);
  1060.  
  1061.         unlock_dir(dir);
  1062.     dput(dentry);
  1063. exit:
  1064.     return error;
  1065. }
  1066.  
  1067. asmlinkage int sys_unlink(const char * pathname)
  1068. {
  1069.     int error;
  1070.     char * tmp;
  1071.  
  1072.     lock_kernel();
  1073.     tmp = getname(pathname);
  1074.     error = PTR_ERR(tmp);
  1075.     if (!IS_ERR(tmp)) {
  1076.         error = do_unlink(tmp);
  1077.         putname(tmp);
  1078.     }
  1079.     unlock_kernel();
  1080.     return error;
  1081. }
  1082.  
  1083. static inline int do_symlink(const char * oldname, const char * newname)
  1084. {
  1085.     int error;
  1086.     struct dentry *dir;
  1087.     struct dentry *dentry;
  1088.  
  1089.     dentry = lookup_dentry(newname, NULL, 0);
  1090.  
  1091.     error = PTR_ERR(dentry);
  1092.     if (IS_ERR(dentry))
  1093.         goto exit;
  1094.  
  1095.     dir = lock_parent(dentry);
  1096.     error = -ENOENT;
  1097.     if (!check_parent(dir, dentry))
  1098.         goto exit_lock;
  1099.  
  1100.     error = may_create(dir->d_inode, dentry);
  1101.     if (error)
  1102.         goto exit_lock;
  1103.  
  1104.     error = -EPERM;
  1105.     if (!dir->d_inode->i_op || !dir->d_inode->i_op->symlink)
  1106.         goto exit_lock;
  1107.  
  1108.     DQUOT_INIT(dir->d_inode);
  1109.     error = dir->d_inode->i_op->symlink(dir->d_inode, dentry, oldname);
  1110.  
  1111. exit_lock:
  1112.     unlock_dir(dir);
  1113.     dput(dentry);
  1114. exit:
  1115.     return error;
  1116. }
  1117.  
  1118. asmlinkage int sys_symlink(const char * oldname, const char * newname)
  1119. {
  1120.     int error;
  1121.     char * from;
  1122.  
  1123.     lock_kernel();
  1124.     from = getname(oldname);
  1125.     error = PTR_ERR(from);
  1126.     if (!IS_ERR(from)) {
  1127.         char * to;
  1128.         to = getname(newname);
  1129.         error = PTR_ERR(to);
  1130.         if (!IS_ERR(to)) {
  1131.             error = do_symlink(from,to);
  1132.             putname(to);
  1133.         }
  1134.         putname(from);
  1135.     }
  1136.     unlock_kernel();
  1137.     return error;
  1138. }
  1139.  
  1140. static inline int do_link(const char * oldname, const char * newname)
  1141. {
  1142.     struct dentry *old_dentry, *new_dentry, *dir;
  1143.     struct inode *inode;
  1144.     int error;
  1145.  
  1146.     /*
  1147.      * Hardlinks are often used in delicate situations.  We avoid
  1148.      * security-related surprises by not following symlinks on the
  1149.      * newname.  --KAB
  1150.      *
  1151.      * We don't follow them on the oldname either to be compatible
  1152.      * with linux 2.0, and to avoid hard-linking to directories
  1153.      * and other special files.  --ADM
  1154.      */
  1155.     old_dentry = lookup_dentry(oldname, NULL, 0);
  1156.     error = PTR_ERR(old_dentry);
  1157.     if (IS_ERR(old_dentry))
  1158.         goto exit;
  1159.  
  1160.     new_dentry = lookup_dentry(newname, NULL, 0);
  1161.     error = PTR_ERR(new_dentry);
  1162.     if (IS_ERR(new_dentry))
  1163.         goto exit_old;
  1164.  
  1165.     dir = lock_parent(new_dentry);
  1166.     error = -ENOENT;
  1167.     if (!check_parent(dir, new_dentry))
  1168.         goto exit_lock;
  1169.  
  1170.     error = -ENOENT;
  1171.     inode = old_dentry->d_inode;
  1172.     if (!inode)
  1173.         goto exit_lock;
  1174.  
  1175.     error = may_create(dir->d_inode, new_dentry);
  1176.     if (error)
  1177.         goto exit_lock;
  1178.  
  1179.     error = -EXDEV;
  1180.     if (dir->d_inode->i_dev != inode->i_dev)
  1181.         goto exit_lock;
  1182.  
  1183.     /*
  1184.      * A link to an append-only or immutable file cannot be created.
  1185.      */
  1186.     error = -EPERM;
  1187.     if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1188.         goto exit_lock;
  1189.  
  1190.     error = -EPERM;
  1191.     if (!dir->d_inode->i_op || !dir->d_inode->i_op->link)
  1192.         goto exit_lock;
  1193.  
  1194.     DQUOT_INIT(dir->d_inode);
  1195.     error = dir->d_inode->i_op->link(old_dentry, dir->d_inode, new_dentry);
  1196.  
  1197. exit_lock:
  1198.     unlock_dir(dir);
  1199.     dput(new_dentry);
  1200. exit_old:
  1201.     dput(old_dentry);
  1202. exit:
  1203.     return error;
  1204. }
  1205.  
  1206. asmlinkage int sys_link(const char * oldname, const char * newname)
  1207. {
  1208.     int error;
  1209.     char * from;
  1210.  
  1211.     lock_kernel();
  1212.     from = getname(oldname);
  1213.     error = PTR_ERR(from);
  1214.     if (!IS_ERR(from)) {
  1215.         char * to;
  1216.         to = getname(newname);
  1217.         error = PTR_ERR(to);
  1218.         if (!IS_ERR(to)) {
  1219.             error = do_link(from,to);
  1220.             putname(to);
  1221.         }
  1222.         putname(from);
  1223.     }
  1224.     unlock_kernel();
  1225.     return error;
  1226. }
  1227.  
  1228. int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  1229.            struct inode *new_dir, struct dentry *new_dentry)
  1230. {
  1231.     int error;
  1232.     int need_rehash = 0;
  1233.  
  1234.     if (old_dentry->d_inode == new_dentry->d_inode)
  1235.         return 0;
  1236.  
  1237.     error = may_delete(old_dir, old_dentry, 1);
  1238.     if (error)
  1239.         return error;
  1240.  
  1241.     if (new_dir->i_dev != old_dir->i_dev)
  1242.         return -EXDEV;
  1243.  
  1244.     if (!new_dentry->d_inode)
  1245.         error = may_create(new_dir, new_dentry);
  1246.     else
  1247.         error = may_delete(new_dir, new_dentry, 1);
  1248.     if (error)
  1249.         return error;
  1250.  
  1251.     if (!old_dir->i_op || !old_dir->i_op->rename)
  1252.         return -EPERM;
  1253.  
  1254.     /*
  1255.      * If we are going to change the parent - check write permissions,
  1256.      * we'll need to flip '..'.
  1257.      */
  1258.     if (new_dir != old_dir) {
  1259.         error = permission(old_dentry->d_inode, MAY_WRITE);
  1260.     }
  1261.     if (error)
  1262.         return error;
  1263.  
  1264.     DQUOT_INIT(old_dir);
  1265.     DQUOT_INIT(new_dir);
  1266.     down(&old_dir->i_sb->s_vfs_rename_sem);
  1267.     error = -EINVAL;
  1268.     if (is_subdir(new_dentry, old_dentry))
  1269.         goto out_unlock;
  1270.     if (new_dentry->d_inode) {
  1271.         error = -EBUSY;
  1272.         if (d_invalidate(new_dentry)<0)
  1273.             goto out_unlock;
  1274.         need_rehash = 1;
  1275.     }
  1276.     error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  1277.     if (need_rehash)
  1278.         d_rehash(new_dentry);
  1279.     if (!error)
  1280.         d_move(old_dentry,new_dentry);
  1281. out_unlock:
  1282.     up(&old_dir->i_sb->s_vfs_rename_sem);
  1283.     return error;
  1284. }
  1285.  
  1286. int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  1287.            struct inode *new_dir, struct dentry *new_dentry)
  1288. {
  1289.     int error;
  1290.  
  1291.     if (old_dentry->d_inode == new_dentry->d_inode)
  1292.         return 0;
  1293.  
  1294.     error = may_delete(old_dir, old_dentry, 0);
  1295.     if (error)
  1296.         return error;
  1297.  
  1298.     if (new_dir->i_dev != old_dir->i_dev)
  1299.         return -EXDEV;
  1300.  
  1301.     if (!new_dentry->d_inode)
  1302.         error = may_create(new_dir, new_dentry);
  1303.     else
  1304.         error = may_delete(new_dir, new_dentry, 0);
  1305.     if (error)
  1306.         return error;
  1307.  
  1308.     if (!old_dir->i_op || !old_dir->i_op->rename)
  1309.         return -EPERM;
  1310.  
  1311.     DQUOT_INIT(old_dir);
  1312.     DQUOT_INIT(new_dir);
  1313.     error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  1314.     if (error)
  1315.         return error;
  1316.     /* The following d_move() should become unconditional */
  1317.     if (!(old_dir->i_sb->s_flags & MS_ODD_RENAME)) {
  1318.         d_move(old_dentry, new_dentry);
  1319.     }
  1320.     return 0;
  1321. }
  1322.  
  1323. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1324.            struct inode *new_dir, struct dentry *new_dentry)
  1325. {
  1326.     if (S_ISDIR(old_dentry->d_inode->i_mode))
  1327.         return vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  1328.     else
  1329.         return vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  1330. }
  1331.  
  1332. static inline int do_rename(const char * oldname, const char * newname)
  1333. {
  1334.     int error;
  1335.     struct dentry * old_dir, * new_dir;
  1336.     struct dentry * old_dentry, *new_dentry;
  1337.  
  1338.     old_dentry = lookup_dentry(oldname, NULL, 0);
  1339.  
  1340.     error = PTR_ERR(old_dentry);
  1341.     if (IS_ERR(old_dentry))
  1342.         goto exit;
  1343.  
  1344.     error = -ENOENT;
  1345.     if (!old_dentry->d_inode)
  1346.         goto exit_old;
  1347.  
  1348.     {
  1349.         unsigned int flags = 0;
  1350.         if (S_ISDIR(old_dentry->d_inode->i_mode))
  1351.             flags = LOOKUP_SLASHOK;
  1352.         new_dentry = lookup_dentry(newname, NULL, flags);
  1353.     }
  1354.  
  1355.     error = PTR_ERR(new_dentry);
  1356.     if (IS_ERR(new_dentry))
  1357.         goto exit_old;
  1358.  
  1359.     new_dir = get_parent(new_dentry);
  1360.     old_dir = get_parent(old_dentry);
  1361.  
  1362.     double_lock(new_dir, old_dir);
  1363.  
  1364.     error = -ENOENT;
  1365.     if (check_parent(old_dir, old_dentry) && check_parent(new_dir, new_dentry))
  1366.         error = vfs_rename(old_dir->d_inode, old_dentry,
  1367.                    new_dir->d_inode, new_dentry);
  1368.  
  1369.     double_unlock(new_dir, old_dir);
  1370.     dput(new_dentry);
  1371. exit_old:
  1372.     dput(old_dentry);
  1373. exit:
  1374.     return error;
  1375. }
  1376.  
  1377. asmlinkage int sys_rename(const char * oldname, const char * newname)
  1378. {
  1379.     int error;
  1380.     char * from;
  1381.  
  1382.     lock_kernel();
  1383.     from = getname(oldname);
  1384.     error = PTR_ERR(from);
  1385.     if (!IS_ERR(from)) {
  1386.         char * to;
  1387.         to = getname(newname);
  1388.         error = PTR_ERR(to);
  1389.         if (!IS_ERR(to)) {
  1390.             error = do_rename(from,to);
  1391.             putname(to);
  1392.         }
  1393.         putname(from);
  1394.     }
  1395.     unlock_kernel();
  1396.     return error;
  1397. }
  1398.